
package plugin_market

import (
	"context"
	"github.com/flipped-aurora/gin-vue-admin/server/global"
	"github.com/flipped-aurora/gin-vue-admin/server/model/plugin_market"
    plugin_marketReq "github.com/flipped-aurora/gin-vue-admin/server/model/plugin_market/request"
    "gorm.io/gorm"
)

type PluginInfoService struct {}
// CreatePluginInfo 创建插件信息管理记录
// Author [yourname](https://github.com/yourname)
func (piService *PluginInfoService) CreatePluginInfo(ctx context.Context, pi *plugin_market.PluginInfo) (err error) {
	err = global.GVA_DB.Create(pi).Error
	return err
}

// DeletePluginInfo 删除插件信息管理记录
// Author [yourname](https://github.com/yourname)
func (piService *PluginInfoService)DeletePluginInfo(ctx context.Context, ID string,userID uint) (err error) {
	err = global.GVA_DB.Transaction(func(tx *gorm.DB) error {
	    if err := tx.Model(&plugin_market.PluginInfo{}).Where("id = ?", ID).Update("deleted_by", userID).Error; err != nil {
              return err
        }
        if err = tx.Delete(&plugin_market.PluginInfo{},"id = ?",ID).Error; err != nil {
              return err
        }
        return nil
	})
	return err
}

// DeletePluginInfoByIds 批量删除插件信息管理记录
// Author [yourname](https://github.com/yourname)
func (piService *PluginInfoService)DeletePluginInfoByIds(ctx context.Context, IDs []string,deleted_by uint) (err error) {
	err = global.GVA_DB.Transaction(func(tx *gorm.DB) error {
	    if err := tx.Model(&plugin_market.PluginInfo{}).Where("id in ?", IDs).Update("deleted_by", deleted_by).Error; err != nil {
            return err
        }
        if err := tx.Where("id in ?", IDs).Delete(&plugin_market.PluginInfo{}).Error; err != nil {
            return err
        }
        return nil
    })
	return err
}

// UpdatePluginInfo 更新插件信息管理记录
// Author [yourname](https://github.com/yourname)
func (piService *PluginInfoService)UpdatePluginInfo(ctx context.Context, pi plugin_market.PluginInfo) (err error) {
	err = global.GVA_DB.Model(&plugin_market.PluginInfo{}).Where("id = ?",pi.ID).Updates(&pi).Error
	return err
}

// GetPluginInfo 根据ID获取插件信息管理记录
// Author [yourname](https://github.com/yourname)
func (piService *PluginInfoService)GetPluginInfo(ctx context.Context, ID string) (pi plugin_market.PluginInfo, err error) {
	err = global.GVA_DB.Where("id = ?", ID).First(&pi).Error
	return
}
// GetPluginInfoInfoList 分页获取插件信息管理记录
// Author [yourname](https://github.com/yourname)
func (piService *PluginInfoService)GetPluginInfoInfoList(ctx context.Context, info plugin_marketReq.PluginInfoSearch) (list []plugin_market.PluginInfo, total int64, err error) {
	limit := info.PageSize
	offset := info.PageSize * (info.Page - 1)
    // 创建db
	db := global.GVA_DB.Model(&plugin_market.PluginInfo{})
    var pis []plugin_market.PluginInfo
    // 如果有条件搜索 下方会自动创建搜索语句
    if len(info.CreatedAtRange) == 2 {
     db = db.Where("created_at BETWEEN ? AND ?", info.CreatedAtRange[0], info.CreatedAtRange[1])
    }
    
    if info.Name != nil && *info.Name != "" {
        db = db.Where("name LIKE ?", "%"+ *info.Name+"%")
    }
    if info.Version != nil && *info.Version != "" {
        db = db.Where("version = ?", *info.Version)
    }
	if info.StartPrice != nil && info.EndPrice != nil {
		db = db.Where("price BETWEEN ? AND ? ", *info.StartPrice, *info.EndPrice)
	}
    if info.Status != "" {
        db = db.Where("status = ?", info.Status)
    }
    if info.Developer != nil && *info.Developer != "" {
        db = db.Where("developer LIKE ?", "%"+ *info.Developer+"%")
    }
    if info.Category != "" {
        db = db.Where("category = ?", info.Category)
    }
    if info.InstallCount != nil {
        db = db.Where("install_count >= ?", *info.InstallCount)
    }
    if info.Rating != nil {
        db = db.Where("rating >= ?", *info.Rating)
    }
	err = db.Count(&total).Error
	if err!=nil {
    	return
    }
        var OrderStr string
        orderMap := make(map[string]bool)
           orderMap["id"] = true
           orderMap["created_at"] = true
         	orderMap["price"] = true
         	orderMap["install_count"] = true
         	orderMap["rating"] = true
       if orderMap[info.Sort] {
          OrderStr = info.Sort
          if info.Order == "descending" {
             OrderStr = OrderStr + " desc"
          }
          db = db.Order(OrderStr)
       }

	if limit != 0 {
       db = db.Limit(limit).Offset(offset)
    }

	err = db.Find(&pis).Error
	return  pis, total, err
}
func (piService *PluginInfoService)GetPluginInfoPublic(ctx context.Context) {
    // 此方法为获取数据源定义的数据
    // 请自行实现
}
